package com.sunshine.lnuplus.view

import android.content.Context
import android.content.DialogInterface
import android.graphics.Color
import android.os.Bundle
import android.provider.Settings
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.base.BaseFragment
import com.sunshine.lnuplus.callback.CloseLessonDetailDialogCallBack
import com.sunshine.lnuplus.callback.EditLessonCallBack
import com.sunshine.lnuplus.model.InitData
import com.sunshine.lnuplus.model.LessonBean
import com.sunshine.lnuplus.model.room.Lesson
import com.sunshine.lnuplus.model.room.TimetableRepository
import com.sunshine.lnuplus.utils.UpdateWidgetUtils
import es.dmoral.toasty.Toasty
import kotlinx.android.synthetic.main.item_lesson_detail.*
import java.util.*

class LessonDetailFragment: BaseFragment() {

    companion object{
        private lateinit var callBack: CloseLessonDetailDialogCallBack
        private lateinit var callBack1: EditLessonCallBack

        fun newInstance(
            lesson: LessonBean,
            callBack: CloseLessonDetailDialogCallBack,
            callBack1: EditLessonCallBack
        ) : LessonDetailFragment{
            this.callBack = callBack
            this.callBack1 = callBack1
            val lessonDetailFragment = LessonDetailFragment()
            val bundle = Bundle().apply {
                putString("lessonId", lesson.lessonId)
                putString("lessonName", lesson.lessonName)
                putString("location", lesson.location)
                putString("teacher", lesson.teacher)
                putInt("dayOfWeek", lesson.dayOfWeek)
                putInt("start", lesson.start)
                putInt("end", lesson.end)
                putString("color", lesson.color)
                putIntegerArrayList("weeks", lesson.weeks as ArrayList<Int>)
            }
            lessonDetailFragment.arguments = bundle
            return lessonDetailFragment
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.item_lesson_detail, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        if(context!!.getSharedPreferences("com.sunshine.lnuplus_preferences", Context.MODE_PRIVATE).getBoolean("night", false)){
            lesson_detail_card.setCardBackgroundColor(Color.BLACK)
            button_edit.setTextColor(Color.WHITE)
            edit_title.setTextColor(Color.WHITE)
        }else{
            lesson_detail_card.setCardBackgroundColor(Color.WHITE)
            button_edit.setTextColor(Color.BLACK)
            edit_title.setTextColor(Color.BLACK)
        }
        lesson_detail_lessonname.text = arguments?.getString("lessonName")
        lesson_detail_location.text = if(arguments?.getString("location").isNullOrEmpty()) "未填写地点" else arguments?.getString("location")
        lesson_detail_teacher.text = if(arguments?.getString("teacher").isNullOrEmpty()) "未填写老师" else arguments?.getString("teacher")
        lesson_detail_time.text = "周${com.sunshine.lnuplus.utils.DateUtils.getTodayOfWeekString(arguments?.getInt("dayOfWeek")!!)} 第${arguments?.getInt("start")}节-第${arguments?.getInt("end")}节"
        lesson_detail_week.text = "${arguments?.getIntegerArrayList("weeks")}"
        val lesson = LessonBean(
            arguments!!.getString("lessonId")!!,
            arguments!!.getString("lessonName")!!,
            arguments!!.getInt("dayOfWeek"),
            arguments!!.getInt("start"),
            arguments!!.getInt("end"),
            arguments!!.getString("location")!!,
            arguments!!.getString("teacher")!!,
            arguments!!.getString("color")!!,
            arguments!!.getIntegerArrayList("weeks")!!
        )
        button_close.setOnClickListener{
            callBack.closeDialog()
        }
        button_edit.setOnClickListener {
            LessonEditDialog.newInstance(
                lesson, arguments!!.getInt("dayOfWeek"),
                arguments!!.getInt("start"),
                arguments!!.getInt("end"),
                callBack,
                callBack1).show(childFragmentManager, "lessonEdit")
        }
        button_delete.setOnClickListener {
            val positive = DialogInterface.OnClickListener { _, _ ->
                val repository = TimetableRepository(context!!)
                InitData.lessonList?.remove(lesson)
                callBack1.updateView()
                repository.deleteLesson(Lesson(
                    arguments!!.getString("lessonId")!!,
                    arguments!!.getInt("dayOfWeek"),
                    arguments!!.getInt("start"),
                    "",
                    arguments!!.getInt("end"),
                    "",
                    arguments!!.getString("lessonName")!!,
                    arguments!!.getString("teacher")!!,
                    arguments!!.getString("location")!!,
                    arguments!!.getIntegerArrayList("weeks").toString().replace("[","".replace("]","")),
                    arguments!!.getString("color")!!
                ))
                repository.deleteCountDown(arguments!!.getString("lessonName")!!)
                UpdateWidgetUtils.updateWidgets(context!!)
                Toasty.success(context!!,"删除成功~",Toasty.LENGTH_SHORT).show()
                callBack.closeDialog()
            }
            DialogWithoutView.newInstance("提醒","你真的要删除这节课吗",false, positive, null).show(childFragmentManager, "delete")
        }
    }

    override fun setContent(): Int {
        return R.layout.item_lesson_detail
    }
}